﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebCoreLib.Extentions
{

    public static class UIExtension
    {
        #region HTML helper

        // Extension method
        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
            anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }



        public static string GetViewData(this HtmlHelper helper, object viewdata, object htmlAttributes)
        {
            if (viewdata != null)
            {
                TagBuilder label = new TagBuilder("label");
                label.InnerHtml = viewdata.ToString();
                label.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

                return label.ToString();
            }
            else
            {
                return string.Empty;
            }

        }


        //public static string ImageLink(this HtmlHelper helper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
        //{
        //    UrlHelper urlHelper = ((Controller)helper.ViewContext.Controller).Url;
        //    string imgtag = helper.ImageLink(imgSrc, alt, imgHtmlAttributes);

        //    //string imgtag = helper.Image(imgSrc, alt, imgHtmlAttributes);
        //    string url = urlHelper.Action(actionName, controllerName, routeValues);

        //    TagBuilder imglink = new TagBuilder("a");
        //    imglink.MergeAttribute("href", url);
        //    imglink.InnerHtml = imgtag;
        //    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

        //    return imglink.ToString();

        //}

        //public static string ImageLinkLightBox(this HtmlHelper helper, string imgSrc, string title, string largeImageSrc, object routeValues, object htmlAttributes, object imgHtmlAttributes)
        //{

        //    string imgtag = helper.Image(imgSrc, title);

        //    TagBuilder imglink = new TagBuilder("a");
        //    imglink.MergeAttribute("href", largeImageSrc);

        //    // title to show details
        //    imglink.MergeAttribute("title", title);
        //    imglink.InnerHtml = imgtag;
        //    imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

        //    return imglink.ToString();

        //}


        /// <summary>
        /// Builds a CheckBoxList with selected items.
        /// </summary>
        /// <param name="Name">Used in to name the collection of checkboxes</param>
        /// <param name="items">List<ListItems> items you want selected</param>
        /// <returns>Html</returns>
        public static string CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {
            return CheckBoxList(helper, name, items, null);
        }

        public static string CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            StringBuilder output = new StringBuilder();

            foreach (var item in items)
            {
                output.Append("<div class=\"fields\"><label>");
                var checkboxList = new TagBuilder("input");
                checkboxList.MergeAttribute("type", "checkbox");
                checkboxList.MergeAttribute("name", name);
                checkboxList.MergeAttribute("value", item.Value);

                // Check to see if it's checked
                if (item.Selected)
                    checkboxList.MergeAttribute("checked", "checked");

                // Add any attributes
                if (checkboxHtmlAttributes != null)
                    checkboxList.MergeAttributes(checkboxHtmlAttributes);

                checkboxList.SetInnerText(item.Text);
                output.Append(checkboxList.ToString(TagRenderMode.SelfClosing));
                output.Append("&nbsp; " + item.Text + "</label></div>");
            }

            return output.ToString();
        }

        public static string GetBaseUrl(this UrlHelper urlHelper)
        {
            Uri contextUri = new Uri(urlHelper.RequestContext.HttpContext.Request.Url, urlHelper.RequestContext.HttpContext.Request.RawUrl);
            UriBuilder realmUri = new UriBuilder(contextUri) { Path = urlHelper.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
            string url = realmUri.Uri.AbsoluteUri;

            if (url.EndsWith("/"))
            {
                url = url.Remove(url.Length - 1, 1);
            }

            return url;
        }




        public static string AbsoluteAction(this HtmlHelper html, string actionUrl)
        {
            Uri requestUrl = html.ViewContext.HttpContext.Request.Url;

            string absoluteAction = string.Format("{0}://{1}{2}",
                                                  requestUrl.Scheme,
                                                  requestUrl.Authority,
                                                  actionUrl);

            return absoluteAction;
        }

        public static string Image(this UrlHelper helper, string fileName)
        {
            return helper.Content(string.Format("~/Content/Images/{0}", fileName));
        }

        public static string Stylesheet(this UrlHelper helper, string fileName)
        {
            return helper.Content(string.Format("~/Content/Css/{0}.css", fileName));
        }

        public static string Javascript(this UrlHelper helper, string fileName)
        {
            return helper.Content(string.Format("~/Content/Scripts/{0}.js", fileName));
        }

        #endregion

    }


}
